import java.io.*;
import java.net.*;
import java.security.*;
// import java.io.BufferedReader;
// import java.io.InputStreamReader;
// import java.io.OutputStreamWriter;
// import java.net.URL;
// import java.net.URLConnection;
// import java.net.URLEncoder;

public class peer
{
	private static int port=8000, maxConnections=0;
	public static void main(String[] argv) throws Exception
	{
		int i=0;
		
		// Listen for incoming connections and handle them
		try
		{
			ServerSocket listener = new ServerSocket(port);
			Socket server;
			while((i++ < maxConnections) || (maxConnections == 0))
			{
				PeerUp connection;

				server = listener.accept();
				PeerUp conn_c= new PeerUp(server);
				Thread t = new Thread(conn_c);
				t.start();
			}
		}
		catch (IOException ioe)
		{
			System.out.println("IOException on socket listen: " + ioe);
			ioe.printStackTrace();
		}
	}
	
	public static String SHA1(String input)
	{
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			md.update(input.getBytes()); 
			byte[] output = md.digest();
			return bytesToHex(output);
		}
		catch (Exception e) {
			System.out.println("Exception: "+e);
			return "Exception: "+e;
		}
	}
	
	public static String bytesToHex(byte[] b)
	{
		char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
						   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
		StringBuffer buf = new StringBuffer();
		for (int j=0; j<b.length; j++)
		{
			buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
			buf.append(hexDigit[b[j] & 0x0f]);
		}
		return buf.toString();
	}
	
	public static String bytesToHex(String b)
	{
		char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
						   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
		StringBuffer buf = new StringBuffer();
		int i;
		for (int j=0; j<b.length(); j++)
		{
			i = (int)b.charAt(j);
			buf.append(hexDigit[(i >> 4) & 0x0f]);
			buf.append(hexDigit[i & 0x0f]);
		}
		return buf.toString();
	}
}

class PeerUp implements Runnable //rencananya mau dipake sebagai server, menerima download-an
{
    private Socket server;
    private String line,input;
	
    PeerUp(Socket server) {
      this.server=server;
    }

    public void run () {
      input="";

      try {
        // Get input from the client
        DataInputStream in = new DataInputStream (server.getInputStream());
        PrintStream out = new PrintStream(server.getOutputStream());
		
		while((line = in.readLine()) != null && !line.equals(".")) {
          input=input + line;
          out.println("I got:" + line);
        }
		
        // Now write to the client
        // System.out.println("Overall message is:" + input);
        // out.println("Overall message is:" + input);
		out.close();
        server.close();
      } catch (IOException ioe) {
        System.out.println("IOException on socket listen: " + ioe);
        ioe.printStackTrace();
      }
	  System.out.println("beres deh\n");
    }
}
